<template>
	<!-- 添加银行卡 -->
	<view class="add_bank_card_main">
		<view class="select_bank">
			<view class="select_bank_item pr">
				<text class="item_name fl font-color">开户银行</text>
				<input 
					type="text"
					value="" 
					v-model="formData.bankname"
					placeholder="请选择您的开户银行"
					placeholder-style="color:#999;font-size: 15px" />
					<image src="@/static/image/datePullDown.png" mode="" class="pull_down pa"></image>
			</view>
			<view class="select_bank_item">
				<text class="item_name fl font-color">银行卡号</text>
				<input 
					type="text"
					value="" 
					v-model="formData.banknum"
					placeholder="请输入银行卡的卡号"
					placeholder-style="color:#999;font-size: 15px" />
			</view>
		</view>
		<view class="bank_card_info">
			<view class="select_bank_item">
				<text class="item_name fl font-color">手机号</text>
				<input 
					type="text"
					value="" 
					v-model="formData.phone"
					placeholder="请输入绑定银行卡的手机号"
					placeholder-style="color:#999;font-size: 15px" />
			</view>
			<view class="select_bank_item pr">
				<text class="item_name fl font-color">验证码</text>
				<input 
					type="text"
					value="" 
					v-model="formData.code"
					placeholder="请输入手机验证码"
					placeholder-style="color:#999;font-size: 15px" />
					<span class="captcha dib tc pa" @click="smsCaptcha()">{{codeText}}</span>
			</view>
			<view class="select_bank_item">
				<text class="item_name fl font-color">持卡人姓名</text>
				<input 
					type="text"
					value="" 
					v-model="formData.name"
					placeholder="请输入持卡人的姓名"
					placeholder-style="color:#999;font-size: 15px" />
			</view>
			<view class="select_bank_item">
				<text class="item_name fl font-color">身份证号</text>
				<input 
					type="text"
					value="" 
					v-model="formData.id_number"
					placeholder="请输入您的身份证号"
					placeholder-style="color:#999;font-size: 15px" />
			</view>
		</view>
		<view class="btns1" @click="submit()">
			提交绑定
		</view>
	</view>
</template>

<script>
	import { isvalidPhone } from '@/utils/validate.js'
	export default {
		data() {
			return {
				bankName:'',
				bankList: ['中国银行', '建设银行'],
				formData: {
					// 开户银行
					bankname: '',
					// 银行卡号
					banknum: '',
					// 手机号
					phone: '',
					// 验证码
					code: '',
					// 持卡人姓名
					name: '',
					// 身份证号
					id_number: ''
				},
				codeText: '获取验证码',
			}
		},
		onLoad() {
		},
		methods: {
			/** 绑定银行卡 */
			submit() {
				/** 开户银行 */
				if (!this.formData.bankname) {
					uni.showToast({
						title: '请选择您的开户银行',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 开户银行卡号 */
				if (!this.formData.banknum) {
					uni.showToast({
						title: '请输入您的银行卡号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 手机号验证 */
				if (!isvalidPhone(this.formData.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 验证码 */
				if (!this.formData.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 持卡人姓名 */
				if (!this.formData.name) {
					uni.showToast({
						title: '请输入持卡人姓名',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 身份证号 */
				if (!this.formData.id_number) {
					uni.showToast({
						title: '请输入身份证号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				let parameter = {
					name: this.formData.name, // 持卡人姓名
					mobile: this.formData.phone, // 手机号
					code: this.formData.code, // 密码
					identity_card: this.formData.id_number, // 身份证号
					credit_card: this.formData.banknum, // 银行卡号
					opening_bank: this.formData.bankname, // 开户行
				}
				this.$post(this.api.bindBankCard, parameter, 'post',  { token: uni.getStorageSync('token') }).then((res) => {
					console.log(res)
					uni.showToast({
						title: '绑定成功',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
				}).catch((err) => {
					console.log(err)
					uni.showToast({
						title: '绑定失败',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
				})
			},
			/** 获取验证码 */
			smsCaptcha() {
				if (this.codeText !== '获取验证码') {
					return
				}
				
				if (!isvalidPhone(this.formData.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 短信验证码倒计时 */
				let temp = 60
				this.codeText = temp + 's'
				let intervalFunc = setInterval(() => {
					if ( temp === 0 ) {
						temp = 0
						this.codeText = '获取验证码'
						clearInterval(intervalFunc)
					} else {
						temp -= 1
						this.codeText = temp + 's'
					}
				}, 1000)
				let parameter = {
					mobile: this.formData.phone, // 手机号
				}
				this.$post(this.api.getBankVerification, parameter, 'post', { token: uni.getStorageSync('token') }).then((res) => {
					console.log(res)
				}).catch((err) => {
					if (!res.success) {
						temp = 0
						this.codeText = '获取验证码'
						clearInterval(intervalFunc)
					}
				})
			},
			/** 选择开户银行 */
			handleSelectBank() {
				
			}
		},
	} 
</script>

<style lang="less" scoped>
	@import "@/asset/less/base.less";
	page, .add_bank_card_main {
		background: #EFEFEF;
		height: 100vh;
	}
	.add_bank_card_main {
		height: 100%;
		.select_bank, .bank_card_info {
			margin-bottom: 40rpx;
			background-color: #fff;
			padding: 0 30rpx;
		}
		.select_bank_item {
			padding: 40rpx 0;
			.item_name {
				font-size: 30rpx;
				width: 160rpx;
				padding-right: 100rpx;
			}
		}
		.btns1 {
			margin: 78rpx 60rpx 0;
		}
		.pull_down {
			width: 18rpx;
			height: 9rpx;
			right: 0;
			top: 50%;
			transform: translateY(-50%);
		}
	}
	.captcha {
		font-size: 24rpx;
		width: 150rpx;
		height: 42rpx;
		line-height: 42rpx;
		border-radius: 10rpx;
		color: #fff;
		right: 0;
		bottom: 50rpx;
		background-color: #D1A764;
	}
</style>
